<template>
	<view class="page">
		<status-bar :title='pageData.class_name' back='0' goHome='1'></status-bar>
		<template v-if='pageData'>
			<view class="lesson-door active-door" @click="call()" style="bottom: 25%;">
				<image :src="staticUrl + '/images/jh_up/server.png'"></image>
			</view>
			
			<view class="flex-box tags" v-if='pageData.info'>
				<view>最多领取<text class="thmeColor" :style="{'--color': color}">{{ pageData.info.send_limit }}</text>人</view>
				<view>剩余领取<text class="thmeColor" :style="{'--color': color}">{{ pageData.info.send_num }}</text>人</view>
			</view>
			<view class="msg-box">
				<view class="title">{{ pageData.class_name }}</view>
				<view class="info">
					<mp-html :content="pageData.class_info" lazy-load scroll-table selectable use-anchor show-with-animation/>
				</view>
				<view class="tag thmeColor" :style="{'--color': color}">课程({{ pageData.lessons?pageData.lessons.length:0 }})</view>
			</view>
			<view class="class-msg">
				<view class="tab-box">
					<view class="tabs flex-box" :class="{fixed: isFixed}" :style="{top: isFixed?navHeight+System.statusBarHeight+'px':0}">
						<view :class="{thmeColor: CurrentIndex == 0}" @click="CurrentIndex = 0" :style="{'--color': color}">课程介绍</view>
						<view :class="{thmeColor: CurrentIndex == 1}" @click="CurrentIndex = 1" :style="{'--color': color}">课程列表</view>
					</view>
				</view>
				<swiper :style="{height:SwipeHeight+'px'}" @change="change1" :current="CurrentIndex">
					<swiper-item>
						<view class="getHeight0">
							<mp-html :content="pageData.class_detail" lazy-load scroll-table selectable use-anchor show-with-animation @load='getSwiperHeight' @ready='getSwiperHeight'/>
						</view>
					</swiper-item>
					<swiper-item>
						<view class="list getHeight1">
							 <!-- :url="'./myCourseList?cid='+item.class_id" hover-class="none" -->
							<view v-if='pageData.lessons && pageData.lessons.length > 0' v-for="(item,index) in pageData.lessons" :key='index' class="lesssonItem">
								<image v-if='item.class_img' @error="item.class_img = ''" :src="item.class_img?imgUrl + item.class_img:staticUrl+'/images/imageLoading.png'" mode="aspectFill" class="lessonImg"></image>
								<view class="flex">
									<view class="tag" :style="{'background-color': color}"></view>
									<view class="lesson-title lesson-title-sm lines">{{item.lesson_title}}</view>
									<view class="lesson-info line2" v-if='item.lesson_info'>{{item.lesson_info}}</view>
									<!-- <view class="tags">
										<text class="green" v-if='item.study > 0'>学习中({{item.study}})</text>
										<text class="red" v-if='item.expire > 0'>已过期({{item.expire}})</text>
									</view> -->
								</view>
							</view>
						</view>
					</swiper-item>
				</swiper>
			</view>
			<view class="btn thmeBg" v-if='pageData.info.buy == 1' :style="{'--color': color}" @click="toDetail">已领取,去查看</view>
			<view class="btn thmeBg" v-else :style="{'--color': color}" @click="getIt">免费领取</view>
		</template>
		<view class="DiLine" v-if='errorMsg'>{{ errorMsg }}</view>
		<backTop v-if='showTop'></backTop>
		<showModel type='2' v-if='showModel' @cancelFunc='showModel=false'></showModel>
		<showModel type='99' v-if='showModel2' :title='title' msg='是否立即前往查看？' btn1='稍后查看' btn2='立即查看'
		 @cancelFunc='showModel2=false' @certain='toDetail'></showModel>
	</view>
</template>

<script>
	import {mapState} from 'vuex';
	let that,fastClick=true;
	export default {
		data() {
			return {
				tab: 1,
				showTop: false,
				staticUrl: this.staticUrl,
				
				color: getApp().globalData.color,
				SwipeHeight: 333,
				CurrentIndex: 0,
				isFixed: false,
				showModel:false,
				showModel2: false,
				c_id:'',
				errorMsg:'',
				pageData: '',
				title:'领取成功!'
			};
		},
		onLoad(e) {
			that = this
			if(e.id){
				that.c_id = e.id
				that.getPagaData()
			}
		},
		methods:{
			toDetail(id){
				let bid = uni.getStorageSync('bid');
				// #ifdef H5
				uni.setStorageSync('goOut','1');// 避免清空缓存信息 否则将拿不到数据
				window.location.href = 'https://sra.weikebaba.com/index.php/singleSaleLesson/index?veriCode='+bid+'&pageType=2&id=' + that.pageData.class_id
				// #endif
			},
			getPagaData(){
				that.$http.get({
					url: '/zzj_singleSaleApi/send_lesson_list',
					data: {
						bid: uni.getStorageSync('bid'),
						v_id: uni.getStorageSync('vid'),
						id: that.c_id
					}
				}).then(res => {
					if(res.code == 100){
						res.data.class_detail = that.$common.richTxt(res.data.class_detail).html
						that.pageData = res.data
						// if(that.pageData.info.buy == 1){
						// 	that.title = '您已领取！'
						// 	that.showModel2 = true
						// }
					}else{
						that.errorMsg = res.msg
					}
				})
			},
			getSwiperHeight(){
				setTimeout(()=>{
					uni.createSelectorQuery().select(".getHeight"+that.CurrentIndex).boundingClientRect((res) => {
					   that.SwipeHeight = res.height<333?333:res.height
					}).exec();
				},100)
			},
			change1(e) {
				that.CurrentIndex = e.detail.current;
				that.getSwiperHeight();
			},
			hasGetIt(){
				uni.showToast({
					title: '你已经领取过了哦 ~',
					icon: 'none'
				})
			},
			getIt(){
				uni.getStorage({
				    key: 'vid', 
					complete(res) {
						if(res.data && res.data != 0){
							if(!fastClick){return false}
							fastClick = false
							that.$http.get({
								url: '/zzj_singleSaleApi/give_lessons',
								data: {
									bid: uni.getStorageSync('bid'),
									v_id: uni.getStorageSync('vid'),
									id: that.c_id
								}
							}).then(res => {
								if(res.code == 100){
									that.showModel2 = true
									that.getPagaData()
								}else{
									fastClick = true
								}
								uni.showToast({
									title:res.msg,
									icon: 'none'
								})
							})
						}else{
							that.showModel = true
						}
					}
				});
				
				
			}
		},
		computed:{
			...mapState(['tabTitle','color','city','navHeight','System','share']),
		},
		onPageScroll(e) {
			if(e.scrollTop > 333){
				that.showTop = true
			}else{
				that.showTop = false
			}
			uni.createSelectorQuery().in(this).select('.tab-box').boundingClientRect(function(data){
				if(data.top <= that.navHeight){
					that.isFixed = true
				}else{
					that.isFixed = false
				}
			}).exec()
		}
	}
</script>

<style lang="scss">
.page{
	background-color: #F5F5F5;
	padding: 30rpx;
	.lesson-door{
		position: fixed;
		right: 20rpx;
		z-index: auto;
		bottom: 38%;
		z-index: 889;
		transform: scale(0);
		transition: all .5s;
		&.active-door{
			transform: scale(1);
		}
		.close-door{
			position: absolute;
			left: 0;top: 0;
			width: 30rpx;height: 30rpx;
			z-index: 999;
		}
		image{
			display: block;
			width: 130rpx;
			height: 130rpx;
		}
	}
	
	.tags{
		flex-wrap: wrap;
		view{
			line-height: 52rpx;
			background-color: #FFFFFF;
			border-radius: 26rpx;
			font-size: 28rpx;
			padding: 0 20rpx;
			margin-right: 20rpx;
			color: #666666;
			text{
				font-size: 28rpx;
				font-weight: bold;
				margin: 0 6rpx;
			}
		}
	}
	.msg-box{
		padding: 24rpx 30rpx;
		background-color: #FFFFFF;
		border-radius: 20rpx;
		margin-top: 20rpx;
		.title{
			color: #333333;
			font-size: 40rpx;
			font-weight: bold;
		}
		.info{
			margin-top: 20rpx;
			color: #999999;
			font-size: 28rpx;
			line-height: 40rpx;
			text-align: justify;
		}
		.tag{
			position: relative;
			line-height: 44rpx;
			height: 44rpx;
			font-size: 26rpx;
			padding: 0 14rpx;
			margin-top: 20rpx;
			&::before{
				content:'';
				position: absolute;
				width: 100%;
				height: 100%;
				top: 0;
				left: 0;
				opacity: 0.25;
				background-color: var(--color);
				border-radius: 44rpx;
			}
		}
	}
	
	.class-msg{
		// padding: 24rpx 30rpx;
		$uni-page-space: 20rpx;
		background-color: #FFFFFF;
		border-radius: 20rpx;
		overflow: hidden;
		margin-top: 20rpx;	
		.tab-box{
			height: 86rpx;
			.tabs{
				line-height: 86rpx;
				background-color: #FFFFFF;
				position: relative;
				z-index: 30;
				&.fixed{
					position: fixed;
					left: 0;
					right: 0;
				}
				view{
					font-size: 28rpx;
					color: #333333;
					padding: 0 30rpx;
					&.thmeColor{
						font-size: 32rpx;
						font-weight: bold;
					}
				}
			}
		}
		
		.getHeight0{
			padding: 30rpx;
			text-align: justify;
		}
		
		.list {
			padding: $uni-page-space 30rpx;
			.lesssonItem{
				padding: $uni-page-space;
				background-color: #FFFFFF;
				border-radius: 8rpx;
				box-shadow:  0 0 3px 0 rgba(0,0,0,.1);
				margin-bottom: $uni-page-space;
				display: flex;
				position: relative;
				.lessonImg{
					width: 200rpx;
					height: 200rpx;
					margin-right: $uni-page-space;
				}
				.tags{
					margin-top: 20rpx;
					text{
						margin-right: 10rpx;
						line-height: 36rpx;
						border-radius: 36rpx;
						height: 36rpx;
						display: inline-block;
						font-size: 24rpx;
						padding: 0 16rpx;
					}
					.green{	
						background-color: #E3FBF0;
						color: #01D475;
					}
					.yellow{
						background-color: #FEF7EB;
						color: #F8AD24;
					}
					.red{
						background-color: #FEEFEF;
						color: #DE5858;
					}
					.gray{
						background-color: #EEEEEE;
						color: #666666;
					}
					.price {
						padding: 0;
						margin-right: 0;
						color: #FF0000;
						font-size: 34rpx;
						font-weight: bold;
					}
				}
				.tag{
					position: absolute;
					width: 24rpx;
					top: 0;
					height: 20rpx;
					right: 20rpx;
					background-color: $tag-bg;
					border-radius: 0 0 8rpx 8rpx;
					margin-bottom: 20rpx;
				}
				.lesson-title{
					font-size: 34rpx;
					font-weight: bold;
					line-height: 48rpx;
					text-align: justify;
				}
				.lesson-title-sm{
					font-size: 28rpx;
				}
				.lesson-info{
					margin-top: 20rpx;
					font-size: 24rpx;
					color: #666666;
					text-align: justify;
				}
				
				// 学习中
				.study{
					padding-right: 70rpx;
				}
				.studying{
					color: #01D475;
					background-color: #E3FBF0;
					line-height: 36rpx;
					font-size: 24rpx;
					height: 36rpx;
					display: block;
					padding: 0 4rpx 0 12rpx;
					border-radius: 32rpx 0 0 32rpx;
					position: absolute;
					right: 0;
					top: 20rpx;
				}
			}
		}
	
	}

	.btn{
		line-height: 88rpx;
		border-radius: 12rpx;
		text-align: center;
		width: 414rpx;
		margin: 100rpx auto;
		color: white;
		font-size: 28rpx;
		background-color: #AFAFAF;
	}
	
}
</style>
